$.ajaxSetup({   
	timeout:30000,
	complete:function(XMLHttpRequest, textStatus) {
			var sessionstatus = XMLHttpRequest.getResponseHeader("SessionStatus");
			if(sessionstatus!="timeout") return;
			window.location.href=ctx + "/login/index.do?redirectURL=" + window.location.href;
	},
	error:function(XMLHttpRequest, textStatus, errorThrown) {
		if(textStatus == "error" || textStatus == "timeout") {
			msg.error("访问出错！网络访问超时。");
		}
	}
});

var frame = {};
//列表数据格式化函数
frame.getFormatter = function(type, format, ctx) {
	var formatter = function(value, rowData, rowIndex) {
		if(type == "Date") {
			var val = new Date(value);
			if(format != null) {
				return val.format(format);
			} else {
				return val.format("yyyy-MM-dd");
			}
		} else if(type == "Time") {
			var val = new Date(value);
			if(format != null) {
				return val.format(format);
			} else {
				return val.format("yyyy-MM-dd hh:mm");
			}
		} else if(type == "DateTime") {
			var val = new Date(value);
			if(format != null) {
				return val.format(format);
			} else {
				return val.format("yyyy-MM-dd hh:mm");
			}
		} else if(type == "UserOrderStatus") {
			if(value >= 100) {
				return "待付款";
			} else if(value == 10){
				return "等待回复";
			} else if(value == 20){
				return "拒绝预约";
			} else if(value == 30){
				return "接受预约";
			} else if(value == 40){
				return "已评价";
			} else if(value == 50){
				return "预约过期";
			}
		} else if(type == "HostOrderStatus") {
			if(value == 10){
				return "未回复";
			} else if(value == 20){
				return "已拒绝";
			} else if(value == 30){
				return "已同意";
			} else if(value == 40){
				return "已评价";
			}  else if(value == 100) {
				return "已发报价";
			} else if(value == 110){
				return "已付款";
			} else if(value == 120){
				return "付款超时";
			} else if(value == 130){
				return "已退款";
			}
		} else if(type == "OrderStatusImg") {
			//rowData.status
			return "<span class=''></span>";
		} else if(type == "MsgAnswer") {
			if(value == 1) {
				return "已回复";
			} else{
				return "<span class='red'>未回复</span>";
			}
		} else if(type == "MsgRead") {
			if(value == 1) {
				return "<span class='icon icon-read'></span>";
			} else{
				return "<span class='icon icon-unread'></span>";
			}
		} else if(type == "ApplyOptions") {
			if(rowData.status >= 300) return;
			return "<a href='javascript:applyVerify(" + rowData.id + ");'>通过审核</a>";
		} else if(type == "msgTitle") {
			if(rowData.orderId) {
				return "<a href='" + ctx + "/order/show.do?id=" + rowData.orderId + "'>" + rowData.msgTitle + "</a>";
			} else {
				return "<a href='" + ctx + "/msg/show.do?id=" + rowData.id + "'>" + rowData.msgContent + "</a>";
			}
		}
	};
	return formatter;
};
//
//$.extend($.fn.form.defaults, {
//	onBeforeLoad:function(){
//		$(this).find("[name]").each(function(){
//			var $input = $(this);
//			var oldName = $input.attr("name"); 
//			if(oldName.indexOf(".") > 0) {
//				$input.attr("mainName", $input.attr("name"));
//				$input.attr("name", oldName.substring(oldName.indexOf(".") + 1, oldName.length));
//			}
//		});
//	},
//	onLoadSuccess:function(){
//		$(this).find("[mainName]").each(function(){
//			var $input = $(this);
//			$input.attr("name",  $input.attr("mainName")).removeAttr("mainName");
//		});
//	},
//	onLoadRestore:function(target){
//		$(target).find("[mainName]").each(function(){
//			var $input = $(this);
//			$input.attr("name",  $input.attr("mainName")).removeAttr("mainName");
//		});
//	}
//});
frame.loadCombo = function(name, val){
	var cc = ['combobox','combotree','combogrid','datetimebox','datebox','combo'];
	var c = $('[comboName="' + name + '"]');
	if (c.length){
		for(var i=0; i<cc.length; i++){
			var type = cc[i];
			if (c.hasClass(type+'-f')){
				if (c[type]('options').multiple){
					c[type]('setValues', val);
				} else {
					c[type]('setValue', val);
				}
				return;
			}
		}
	}
};
/**
 * 载入form表单
 */
frame.onLoadForm = function(target, objAttr, data) {
	var form = $(target);
	for(var key in data){
		var name = objAttr + "." + key;
		var val = data[key];
		var rr = _checkField(name, val);
		if (!rr.length){
			var f = form.find('input[numberboxName="'+name+'"]');
			if (f.length){
				f.numberbox('setValue', val);	// set numberbox value
			} else {
				$('input[name="'+name+'"]', form).val(val);
				$('textarea[name="'+name+'"]', form).val(val);
				$('select[name="'+name+'"]', form).val(val);
			}
		}
		_loadCombo(name, val);
	}
	
	function _loadCombo(name, val){
		var form = $(target);
		var cc = ['combobox','combotree','combogrid','datetimebox','datebox','combo'];
		var c = form.find('[comboName="' + name + '"]');
		if (c.length){
			for(var i=0; i<cc.length; i++){
				var type = cc[i];
				if (c.hasClass(type+'-f')){
					if (c[type]('options').multiple){
						c[type]('setValues', val);
					} else if("datebox" == type && val) {
						c[type]('setValue', (new Date(val)).format("yyyy-MM-dd"));
					} else if("datetimebox" == type && val) {
						c[type]('setValue', (new Date(val)).format("yyyy-MM-dd hh:mm"));
					} else {
						c[type]('setValue', val);
					}
					return;
				}
			}
		}
	}
	
	/**
	 * check the checkbox and radio fields
	 */
	function _checkField(name, val){
		var rr = $(target).find('input[name="'+name+'"][type=radio], input[name="'+name+'"][type=checkbox]');
		rr._propAttr('checked', false);
		rr.each(function(){
			var f = $(this);
			if (f.val() == String(val) || $.inArray(f.val(), val) >= 0){
				f._propAttr('checked', true);
			}
		});
		return rr;
	}
};
/**
 * 提交form表单
 */
frame.submitForm = function(formId, butObj, success, onsubmit) {
	$("#" + formId).form('submit', {
		onSubmit: function(param) {
			if(!$(this).form('validate')) return false;
			if($.isFunction(onsubmit)) {
				return onsubmit(param);
			}
			if(butObj)$(butObj).linkbutton({}).linkbutton("disable");
	    },
	    success: function(res) {
	    	success(tools.s2j(res));
	    	if(butObj)$(butObj).linkbutton({}).linkbutton("enable");
	    }
	}); 
};